package trial.hel;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @创建人 MBY381
 * @创建时间 2022/4/28 12:49
 * @描述 暂无
 */
public class Huawei {
    public static void main(String[] args) {

        int[] nums=new int[]{1,2,3};
        int[] nums1= Arrays.copyOf(nums,nums.length);
        System.out.println(Arrays.toString(nums1));
        System.out.println((int) 'z');

        String[] strs = new String[]{"mby", "zzq", "zwj","hxy"};
        String lib = "mbyzzqmbyhx";
        System.out.println(test(strs, lib));
//        System.out.println(xiaoliang(strs, lib));

        String[] strs1 = new String[]{"cat", "bt", "hat", "tree"};
        String lib1 = "atach";
        System.out.println(test(strs1, lib1));
//        System.out.println(xiaoliang(strs1, lib1));
    }

    public static int test(String[] strs, String lib){
        byte[] libNums=new byte[26];
        Arrays.fill(libNums, (byte) 0);
        for (int i = 0; i < lib.length(); i++) {
            libNums[lib.charAt(i)-97]++;
        }
        int ans=0;
        for (String s:strs) {
            byte[] cost=Arrays.copyOf(libNums,libNums.length);
            int len=s.length();
            boolean valid=true;
            for (int i = 0; i < len; i++) {
                int ch=s.charAt(i)-97;
                if(cost[ch]>0){
                    cost[ch]--;
                }else {
                    valid=false;
                    break;
                }
            }
            if(valid){
                ans+=len;
            }
        }
        System.out.println(Arrays.toString(libNums));
        return ans;
    }

    public static int xiaoliang(String[] strs, String lib) {
        Map<Character, Integer> map = new TreeMap<>();
        for (int i = 0; i < lib.length(); i++) {
            char ch = lib.charAt(i);
            if (map.containsKey(ch)) {
                map.put(ch, map.get(ch) + 1);
            } else {
                map.put(ch, 1);
            }
        }
        int ans = 0;

        for (String str : strs) {
            Map<Character, Integer> tempMap = new HashMap<>(Map.copyOf(map));
            boolean valid = true;
            int len = str.length();
            for (int i = 0; i < len; i++) {
                char ch = str.charAt(i);
                if (map.containsKey(ch)) {
                    int left = tempMap.get(ch);
                    if (ch > 0) {
                        tempMap.put(str.charAt(i), left - 1);
                    } else {
                        valid = false;
                        break;
                    }
                } else {
                    valid = false;
                    break;
                }
            }
            if (valid) {
                ans += len;
            }
        }
        System.out.println(map);
        return ans;
    }
}
